package com.yidu.logisticssystem.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.logisticssystem.dao.*;
import com.yidu.logisticssystem.entity.*;
import com.yidu.logisticssystem.entity.vo.ChargeVo;
import com.yidu.logisticssystem.entity.vo.WaybillVo;
import com.yidu.logisticssystem.service.LogisticsWaybillService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 货运单(LogisticsWaybill)表服务实现类
 *
 * @author yuxigan
 * @since 2021-03-08 16:46:58
 */
@Service("logisticsWaybillService")
public class LogisticsWaybillServiceImpl implements LogisticsWaybillService {
    @Resource
    private LogisticsWaybillDao logisticsWaybillDao;
    @Resource
    private LogisticsChargeDao chargeDao;
    @Resource
    private LogisticsReceiptDao receiptDao;
    @Resource
    private LogisticsMapDao mapDao;
    @Resource
    private LogisticsStateDao stateDao;

    /**
     * 通过ID查询单条数据
     *
     * @param wayBillid 主键
     * @return 实例对象
     */
    @Override
    public LogisticsWaybill queryById(Integer wayBillid) {
        return this.logisticsWaybillDao.queryById(wayBillid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsWaybill> queryAllByLimit(int offset, int limit) {
        return this.logisticsWaybillDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param vo 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional //事务注解
    public int insert(WaybillVo vo, HttpServletRequest request) {
        //先根据发货地区id跟收货地区id查找出所在的城市
        //把发货地址分割出来
        String []maps=vo.getSbConsignarea().split(",");
        //把收货地址分割出来
        String []tomaps=vo.getSbSendarea().split(",");
        //根据地区id查询出收货地点
        LogisticsMap tomap=mapDao.queryById(tomaps[0]);
        //根据地区id查询出发货地点
        LogisticsMap map=mapDao.queryById(maps[0]);
        //获取发货地点的城市id
        List<LogisticsMap> list=mapDao.selectfufind(map.getMapId());
        String mapid="";
        for(LogisticsMap map1:list){
            mapid=map1.getMapId();
        }
        //获取收货地点的城市id
        List<LogisticsMap> listcity=mapDao.selectfufind(tomap.getMapId());
        String tomapid="";
        for(LogisticsMap map2:listcity){
            tomapid=map2.getMapId();
        }
        //new一个路线收费标准表
        LogisticsCharge logisticsCharge=new LogisticsCharge();
        //开始城市
        logisticsCharge.setChargeStart(mapid);
        //到达城市
        logisticsCharge.setChargeOver(tomapid);
        //根据发货地址跟收货地址查询收费标准
        LogisticsCharge charge=chargeDao.queryByMapId(logisticsCharge);
        Gson gson=new Gson();
        String merchandise=charge.getChargePrice();
        //将收费标准(字符串)转化成ChargeVo对象
        ChargeVo vo1 =gson.fromJson(merchandise,ChargeVo.class);
        //把重量转成Doule类型取出来
        Double weight=Double.valueOf(vo.getReceiptWeight());
        Double jiage=0.0;
        //根据路线计算标准计算出运费
        jiage+=vo1.getFirst();
        if(weight>1&&weight-2<0){
            jiage+=(weight-1)*vo1.getOne();
        }else {
            jiage+=vo1.getOne();
        }
        if(weight-2>0&&weight-5<=0){
            jiage+=(weight-2)*vo1.getTwo();
        }else {
            jiage+=vo1.getTwo();
        }
        if(weight-5>0){
            jiage+=(weight-5)*vo1.getFive();
        }

        //------------------货运单回执-------------------
        LogisticsReceipt receipt=new LogisticsReceipt();
        //加入货运单编号
        receipt.setReceiptWaynum(vo.getWayNum());
        //加入件数
        receipt.setReceiptNumber(vo.getReceiptNumber());
        //加入重量
        receipt.setReceiptWeight(weight);
        //加入计费标准
        receipt.setReceiptBilling("首重(1kg):"+vo1.getFirst()+" " +
                ",2kg以内(包括2kg):"+vo1.getOne()+",大于2kg小于等于5kg:"+ vo1.getTwo()
                +" ,5kg以外的:"+vo1.getFive());
        //加入计价方式
        receipt.setReceiptMode("称重");
        //添加货运单回执
        receiptDao.insert(receipt);

        //----------修改货运状态---------
        //通过货运单编号跟状态明细id修改完成进度
        LogisticsState sta=new LogisticsState();
        //货运单编号
        sta.setWayNum(vo.getWayNum());
        //完成状态
        sta.setStaTai(1);
        //状态明细id
        sta.setDetailtypeId(1);
        //从session取出员工对象
        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");
        //设置操作员工id
        sta.setStaffId(staff.getStaffId());
        stateDao.updateWay(sta);

        //----------------新增货运状态---------------
        LogisticsState state=new LogisticsState();
        //设置状态明细id
        state.setDetailtypeId(2);
        //设置员工id
        state.setStaffId(1);
        //设置状态
        state.setStaTai(0);
        //设置货运单编号
        state.setWayNum(vo.getWayNum());
        //设置发生时间
        Date date = new Date();
        SimpleDateFormat si=new SimpleDateFormat("yyyy-MM-dd");
        String dates=si.format(date);
        state.setStateTime(java.sql.Date.valueOf(dates));
        //设置备注
        state.setStateRemark(vo.getStateRemark());
        //加入货运状态
        stateDao.insert(state);

        //---------------------货运单--------------------------
        LogisticsWaybill waybill=new LogisticsWaybill();
        //设置货运单编号
        waybill.setWayNum(vo.getWayNum());
        //设置发货人姓名
        waybill.setWaySendname(vo.getSbSendname());
        //设置发货人地址
        waybill.setWaySendarea(vo.getSbSendarea());
        //设置发货人电话
        waybill.setWaySendphone(vo.getSbSendphone());
        //设置收货人姓名
        waybill.setWayConsignname(vo.getSbConsignname());
        //设置收货人地址
        waybill.setWayConsignarea(vo.getSbConsignarea());
        //设置收货人电话
        waybill.setWayConsignphone(vo.getSbConsignphone());
        //设置货物类型
        waybill.setWaytypeId(vo.getWaytypeId());
        //设置状态id
        waybill.setStateId(state.getZiId());
        //设置运费
        waybill.setWayFreight(jiage);
        //设置运费计算方式
        waybill.setWayFreightmode(vo.getSbFreightmode());
        //设置备注
        waybill.setWayRemark(vo.getStateRemark());
        return logisticsWaybillDao.insert(waybill);
    }

    /**
     * 修改数据
     *
     * @param logisticsWaybill 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsWaybill update(LogisticsWaybill logisticsWaybill) {
        this.logisticsWaybillDao.update(logisticsWaybill);
        return this.queryById(logisticsWaybill.getWayBillid());
    }




    /**
     * 通过主键删除数据
     *
     * @param wayBillid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer wayBillid) {
        return this.logisticsWaybillDao.deleteById(wayBillid) > 0;
    }

    @Override
    public LogisticsWaybill way_num(String wayNum) {
        return logisticsWaybillDao.way_num(wayNum);
    }
}
